﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing.Imaging;

namespace LibraryUtility.Web
{
    /// <summary>
    /// ClassName:SystemCode
    /// Auther:Leo.wl
    /// Time:2010-04-14
    /// </summary>
    public class SystemCode
    {
        public SystemCode()
        {
        }

        //MD5加密。
        private static readonly string md5Key = ConfigHelper.GetConfigString("Md5Key");

        #region   密码加密
        /// <summary>
        /// MD5s the specified STR.
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <param name="code">The code.</param>
        /// <returns></returns>
        public static string md5(string str, int code)
        {
            if (code == 16) //16位MD5加密（取32位加密的9~25字符） 
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, md5Key).ToLower().Substring(8, 16);
            }
            else//32位加密 
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, md5Key).ToLower();
            }
        }

        #endregion

        #region   图片上传

        /// <summary>
        /// Ups the load.
        /// </summary>
        /// <param name="MyPostedFile">My posted file.</param>
        /// <param name="height">The height.</param>
        /// <param name="width">The width.</param>
        /// <param name="savePath">The save path.</param>
        /// <returns></returns>
        public string UpLoad(HttpPostedFile MyPostedFile, int height, int width, string savePath)
        {

            if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)
            {
                HttpContext.Current.Response.Write("<script>alert('无效的图形格式');</script>");
                return "0";
            }
            string url = GetThumbNail(MyPostedFile.FileName, height, width, MyPostedFile.ContentType.ToString(), false, MyPostedFile.InputStream, savePath);
            return url;
        }

        /// <summary>
        /// Gets the thumb nail.
        /// </summary>
        /// <param name="strFileName">Name of the STR file.</param>
        /// <param name="iWidth">Width of the i.</param>
        /// <param name="iheight">The iheight.</param>
        /// <param name="strContentType">Type of the STR content.</param>
        /// <param name="blnGetFromFile">if set to <c>true</c> [BLN get from file].</param>
        /// <param name="ImgStream">The img stream.</param>
        /// <param name="savePath">The save path.</param>
        /// <returns></returns>
        private string GetThumbNail(string strFileName, int iWidth, int iheight, string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream, string savePath)
        {
            System.Drawing.Image oImg;
            if (blnGetFromFile)
            {
                oImg = System.Drawing.Image.FromFile(strFileName);
            }
            else
            {
                oImg = System.Drawing.Image.FromStream(ImgStream);
            }
            oImg = oImg.GetThumbnailImage(iWidth, iheight, null, IntPtr.Zero);
            string strGuid = System.Guid.NewGuid().ToString().ToUpper();
            string strFileExt = strFileName.Substring(strFileName.LastIndexOf(".")).ToLower();
            HttpContext.Current.Response.ContentType = strContentType;
            MemoryStream MemStream = new MemoryStream();
            string HardWarePath = HttpContext.Current.Server.MapPath(savePath) + strGuid + strFileExt;
            oImg.Save(HardWarePath, GetImageType(strContentType));
            //MemStream.WriteTo(HttpContext.Current.Response.OutputStream);
            return savePath + strGuid + strFileExt;
        }
        
        /// <summary>
        /// Gets the type of the image.
        /// </summary>
        /// <param name="strContentType">Type of the STR content.</param>
        /// <returns></returns>
        private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType)
        {
            if ((strContentType.ToString().ToLower()) == "image/pjpeg")
            {
                return System.Drawing.Imaging.ImageFormat.Jpeg;
            }
            else if ((strContentType.ToString().ToLower()) == "image/gif")
            {
                return System.Drawing.Imaging.ImageFormat.Gif;
            }
            else if ((strContentType.ToString().ToLower()) == "image/bmp")
            {
                return System.Drawing.Imaging.ImageFormat.Bmp;
            }
            else if ((strContentType.ToString().ToLower()) == "image/tiff")
            {
                return System.Drawing.Imaging.ImageFormat.Tiff;
            }
            else if ((strContentType.ToString().ToLower()) == "image/x-icon")
            {
                return System.Drawing.Imaging.ImageFormat.Icon;
            }
            else if ((strContentType.ToString().ToLower()) == "image/x-png")
            {
                return System.Drawing.Imaging.ImageFormat.Png;
            }
            else if ((strContentType.ToString().ToLower()) == "image/x-emf")
            {
                return System.Drawing.Imaging.ImageFormat.Emf;
            }
            else if ((strContentType.ToString().ToLower()) == "image/x-exif")
            {
                return System.Drawing.Imaging.ImageFormat.Exif;
            }
            else if ((strContentType.ToString().ToLower()) == "image/x-wmf")
            {
                return System.Drawing.Imaging.ImageFormat.Wmf;
            }
            else
            {
                return System.Drawing.Imaging.ImageFormat.MemoryBmp;
            }
        }

        #endregion
    }
}
